package ru.amse.baltijsky.javascheme.nodeshape;

import java.util.ArrayList;
import java.util.List;

/**
 * Traits of the schema tree nodes as visualizable objects.
 * Is intended to be stored inside a Node as an object of a specific implementing subclass.
 */
public abstract class NodeVisualTraits {
    /**
     * Node shape.
     */
    private INodeShape shape = null;
    /**
     * Maximum shape widths for the children of the node (for all available levels).
     */
    private List<Integer> childrenWidths = new ArrayList<Integer>();

    /**
     * Gets the shape of the node.
     *
     * @return node shape
     */
    public INodeShape getShape() {
        return shape;
    }

    /**
     * Sets the correct shape as the node trait. The cocrete shape is determined by the implementing subclass.
     */
    public abstract void setShape();

    /**
     * Used to reset the shape using the newly created shape.
     *
     * @param theShape the shape to set
     */
    protected void setShape(INodeShape theShape) {
        shape = theShape;
    }

    /**
     * Gets the whole list of children widths.
     *
     * @return the list of node's children widths
     */
    public Iterable<Integer> getChildrenWidths() {
        return childrenWidths;
    }

    /**
     * Checks the list for the children widths at the specified position and, if there is an
     * element, compares it to the given value, storing the maximum. If there's no element at
     * the specified position, simply stores the value.
     *
     * @param e   child width (the value)
     * @param pos child number (position in the children list)
     */
    public void setMaxChildWidth(Integer e, int pos) {
        while (childrenWidths.size() - 1 < pos) {
            childrenWidths.add(0);
        }
        if (childrenWidths.get(pos) <= e) {
            childrenWidths.set(pos, e);
        }
    }
}
